/*
 * mysocket.h
 *
 *  Created on: 2009-3-31
 *      Author: tian
 */

#ifndef MYSOCKET_H_
#define MYSOCKET_H_

#include"ipaddr.h"
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/stat.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define BUFLEN 100
#define MAXSOCKET 10

long tcpipstackqid;
long processqid;
int begin[MAXSOCKET];
int end[MAXSOCKET];
char * sockbuf[MAXSOCKET];

struct sockaddr{
	struct in_addr peer_ip;
	struct in_addr my_ip;
	unsigned short peer_port;
	unsigned short my_port;
};

typedef struct mymsg{
	long mtype;
	pid_t pid;
	int sock;
	int msgqid;
	//struct sockaddr sock_addr;
	unsigned int buflen;
	char buf[BUFLEN];
}msg;

typedef struct sockmsg{
	long mtype; /*socket number*/
	int ret;	 /*return value*/
	struct sockaddr sock_addr;
	pid_t pid;
}sockmsg;

/* this function create a new socket
 * Return Values:it returns a negative integer if
 * some error happens,and a positive number which
 * presents the socket is returned if succeed.And
 * this number is called the socket's file descriptor.
 */
int socket();

/* this function close a socket and release all
 * resource associated with this socket, including
 * send FIN package to the peer host.
 * Parameter:
 * sockfd:the socket's file descriptor
 * Return Values:
 * it returns a negative integer if some error is
 * happen, and 0 is returned if succeed.
 */
int close(int sockfd);

/* this function reads data from a socket to a
 * buffer
 * Parameter:
 * sockfd:the socket's file descriptor
 * buf:a pointer points to a buffer
 * bufsize:the size of buffer
 * Return Values:
 * it returns a negative integer if some error is
 * happen, and 0 is returned if FIN is received(we
 * can see this as somewhat succeed but not error).
 * when it is succeed, it returns the number of
 * bytes it has read.
 */
int readfromsocket(int sockfd,char *buf,int bufsize);

/* this function write data from a buffer to a
 * socket
 * Parameter:
 * sockfd:the socket's file descriptor
 * buf:a pointer points to a buffer
 * buflen:the length of buffer
 * Return Values:
 * it returns a negative integer if some error is
 * happen, the number of bytes it has wrote to the
 * socket is returned.
 */
int writetosocket(int sockfd,char *buf,int buflen);

/* this function binds a socket to a ip address and
 * port pair.a socket can not call listen() to listen
 * on a port until bind() is called.
 * Parameter:
 * sockfd:the socket's file descriptor
 * myaddr:a pointer to a struct contains ip address
 * and port
 * addrlen:the size of myaddr (however, in our design,
 * this parameter is constant-8 bytes)
 * Return Values:
 * it returns a negative integer if some error is
 * happen, and 0 is returned if it succeed.
 */
int bind(int sockfd,const struct sockaddr *myaddr,int addrlen);

/* this function connects to another host.after this,
 * a user can use the sockfd to transfer data.
 * Parameter:
 * sockfd:the socket's file descriptor
 * servaddr:a pointer points to a struct contains ip
 * address and port which presents a node in the net.
 * addrlen:the size of myaddr (however, in our design,
 * this parameter is constant-8 bytes)
 * Return Values:
 * it returns a negative integer if some error is
 * happen, and 0 is returned if it succeed.
 */
int connect(int sockfd,const struct sockaddr *servaddr,int addrlen);

/* this function makes a socket listen on a port,
 * a server can not call accept() until it is called.
 * Parameter:
 * sockfd:the socket's file descriptor
 * backlog:the length of listen queue(maybe we will not
 * implement this, then this parameter is useless)
 * Return Values:
 * it returns a negative integer if some error is
 * happen, and 0 is returned if it succeed.
 */
int listen(int sockfd,int backlog);

/* this function accepts connect from a host,it will
 * block itself until some client connects it
 * Parameter:
 * sockfd:the listening socket's file descriptor
 * cliaddr:a pointer points to a struct sockaddr,
 * which is used to save peer host ip address and
 * port.This is also can be NULL, if user is not interested
 * in such information.
 * addrlen:a pointer to an integer which contains the size
 * of the space pointed to by cliaddr
 * Return Values:
 * it returns a socket's file descriptor which is connected
 * to the peer host.users can use this to transfer data to
 * the peer host.And cliaddr contains the peer host's ip
 * address and port while addrlen the size of *cliaddr.However,
 * in our design, there is no other struct which includes ip
 * address and port excpet sockaddr.As a result *addrlen is
 * always a constant(8 bytes).
 */
int accept(int sockfd,struct sockaddr *cliaddr,int *addrlen);
int sock_init();

#endif /* MYSOCKET_H_ */
